package com.openway.game.view;

import com.openway.game.personage.GameObject;
import com.openway.game.personage.impl.*;

/**
 * Abstract base class for {@link View} implementations, that display the state of the game in a pseudo-graph
 *
 * @author Created: 29.02.12 13:50
 * @version $Date$ $Revision$
 */
public abstract class AbstractPseudoGraphView implements View {

    protected static final char EMPTY = '0';
    protected static final char DRINKER_WALKING = '@';
    protected static final char DRINKER_SLEEP_STANDING = '1';
    protected static final char DRINKER_SLEEP_LYING = '&';
    protected static final char POST = '#';
    protected static final char LAMP = 'L';
    protected static final char BOTTLE = 'B';
    protected static final char POLICEMAN = '!';

    private String description;

    public AbstractPseudoGraphView() {
    }

    protected String getDescription() {
        if (description == null) {
            StringBuilder descriptionBuilder = new StringBuilder();
            descriptionBuilder.append(EMPTY).append(" - empty cell").append("\n");
            descriptionBuilder.append(DRINKER_WALKING).append(" - walking drinker").append("\n");
            descriptionBuilder.append(DRINKER_SLEEP_STANDING).append(" - drinker sleep standing").append("\n");
            descriptionBuilder.append(DRINKER_SLEEP_LYING).append(" - drinker sleep lying").append("\n");
            descriptionBuilder.append(POST).append(" - post").append("\n");
            descriptionBuilder.append(LAMP).append(" - lamp").append("\n");
            descriptionBuilder.append(BOTTLE).append(" - bottle").append("\n");
            descriptionBuilder.append(POLICEMAN).append(" - policeman").append("\n");

            this.description = descriptionBuilder.toString();
        }
        return this.description;
    }

    protected char getObjectSymbol(GameObject gameObject) {
        if (gameObject instanceof Bottle)
            return BOTTLE;
        if (gameObject instanceof Post)
            return POST;
        if (gameObject instanceof Lamp)
            return LAMP;
        if (gameObject instanceof Drunkard) {
            Drunkard drunkard = (Drunkard) gameObject;
            if (drunkard.isWalking())
                return DRINKER_WALKING;
            if (drunkard.isSleepLying())
                return DRINKER_SLEEP_LYING;
            if (drunkard.isSleepStanding())
                return DRINKER_SLEEP_STANDING;
        }
        if (gameObject instanceof Policeman) {
            return POLICEMAN;
        }
        return EMPTY;
    }
}
